package com.xrit.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xrit.constant.MessageConstant;
import com.xrit.exception.CustomException;
import com.xrit.mapper.CheckItemMapper;
import com.xrit.pojo.CheckItem;
import com.xrit.query.QueryPageBean;
import com.xrit.result.PageResult;
import com.xrit.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service//(interfaceClass = CheckItemService.class)
public class CheckItemServiceImpl implements CheckItemService{

    @Autowired
    private CheckItemMapper checkItemMapper;

    @Override
    public int delete(Integer id) throws CustomException {
        //首先检查是否又检查组的关联
        int count = checkItemMapper.selectCheckItemAndCheckGroup(id);
        if (count>0){
            throw new CustomException(MessageConstant.CHECKITEM_IN_USE);
        }
        return checkItemMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(CheckItem checkItem) {
        return checkItemMapper.updateByPrimaryKeySelective(checkItem);
    }

    @Override
    public int add(CheckItem checkItem) {
        return checkItemMapper.insertSelective(checkItem);
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean){

        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),true);
        Page<CheckItem> page = checkItemMapper.selectByQueryString(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<CheckItem> findAll() {
        return checkItemMapper.selectByExample(null);
    }
}
